"ui";

// 创建UI
ui.layout(
    <vertical>
        <button id="start">启动服务</button>
        <button id="stop">停止服务</button>
    </vertical>
);

// 定义服务
let service;

// 启动前台服务
ui.start.on("click", () => {
    service = threads.start(() => {
        // 设置前台服务
        if (foregroundService) {
            foregroundService.start("My Auto.js Service", "Service is running...");
        }
        while (true) {
            log("Service is running...");
            sleep(1000); // 每秒执行一次
        }
    });
    toast("服务已启动");
});

// 停止服务
ui.stop.on("click", () => {
    if (service) {
        service.interrupt(); // 结束线程
        service = null;
        toast("服务已停止");
    }
});
